//
//  YLPhotoBrower.m
//  chijidun
//
//  Created by iMac on 17/1/9.
//
//

#import "YLPhotoBrower.h"

@interface YLPhotoBrower ()
{
    UIScrollView *m_scrollView;
    UIButton *m_deleteBtn;
    NSInteger _currentIndex;
}
@end
@implementation YLPhotoBrower

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addMainViewWith:frame];
    }
    return self;
}
-(void)addMainViewWith:(CGRect)frame
{
    [self setBackgroundColor:[UIColor blackColor]];
    
    m_scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    [m_scrollView setBackgroundColor:[UIColor clearColor]];
    m_scrollView.pagingEnabled=YES;
    m_scrollView.bounces = NO;
    m_scrollView.delegate=self;
    [self addSubview:m_scrollView];
    
    m_deleteBtn=[[UIButton alloc]initWithFrame:CGRectMake(W(self)-60, H(self)-60, 40, 40)];
    [m_deleteBtn setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [m_deleteBtn setImage:[UIImage imageNamed:@"image_delete"] forState:UIControlStateNormal];
    [m_deleteBtn addTarget:self action:@selector(deleteBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:m_deleteBtn];
}
-(void)hideDeleteBtn
{
    [m_deleteBtn setHidden:YES];
}
-(void)reloadDatasWith:(BOOL)load
{
    [m_scrollView setContentSize:CGSizeMake(kMainScreen_Width*_photos.count, kMainScreen_Height)];
    for (int i=0; i<_photos.count; i++) {
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(kMainScreen_Width*i, 0, W(m_scrollView), H(m_scrollView))];
        if (load) {
            [imageView load:_photos[i]];
        }else{
            [imageView setImage:_photos[i]];
        }
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        [imageView click:self action:@selector(photoViewSingleTap)];
        [m_scrollView addSubview:imageView];
    }
    [m_scrollView  setContentOffset:CGPointMake(kMainScreen_Width*_currentIndex, 0) animated:NO];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width;
    _currentIndex=index;
}

-(void)show
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    self.alpha = 0;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    }];

}
- (void)photoViewSingleTap
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)deleteBtnClicked:(id)sender
{
    [_photos removeObjectAtIndex:_currentIndex];
    if (_photos.count>0) {
        if (self.removeBlock) {
            self.removeBlock(_currentIndex);
        }
        
        if (_currentIndex==_photos.count) {
            _currentIndex=0;
        }
        [self reloadDatasWith:NO];
    }else{
        if (self.removeBlock) {
            self.removeBlock(_currentIndex);
        }
        [self photoViewSingleTap];
    }
}


@end
